﻿////////////////////////////////////////////////////////////////////////////////
// Source Copyright (C) J.Fan
// @author      J.Fan
// @date        2013-02-08
// @revision    Revision in Version Control System
// @remark      
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Globalization;

namespace JsonConverter.Converter
{
    /// <summary>
    /// Convert DateTime value to a string like the result of
    /// JavaScript Date.toString(). It likes
    /// Sat Jan 19 2013 12:34:56 +0800
    /// </summary>
    public class DateTimeJavaScriptConverter: IPrimitiveConverter
    {
        public const string PATTERN = @"""{0} {1:+00;-00}{2:00}""";

        public string Convert(object value)
        {
            TimeSpan offset = TimeZoneInfo.Local.BaseUtcOffset;
            return string.Format(PATTERN,
                ((DateTime) value).ToString("ddd MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture),
                offset.Hours,
                offset.Minutes);
        }
    }
}
